package org.jivesoftware.smackx.workgroup.packet;

import java.util.HashMap;
import java.util.Map;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.provider.IQProvider;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smackx.workgroup.agent.InvitationRequest;
import org.jivesoftware.smackx.workgroup.agent.OfferContent;
import org.jivesoftware.smackx.workgroup.agent.TransferRequest;
import org.jivesoftware.smackx.workgroup.agent.UserRequest;
import org.jivesoftware.smackx.workgroup.util.MetaDataUtils;
import org.xmlpull.v1.XmlPullParser;

public class OfferRequestProvider
  implements IQProvider
{
  public IQ parseIQ(XmlPullParser paramXmlPullParser)
    throws Exception
  {
    paramXmlPullParser.getEventType();
    String str1 = null;
    int i = -1;
    Object localObject1 = null;
    int j = 0;
    Object localObject2 = new HashMap();
    String str2 = paramXmlPullParser.getAttributeValue("", "jid");
    String str3 = str2;
    while (true)
    {
      int k;
      label336: 
      do
        while (true)
        {
          String str4;
          do
            while (true)
            {
              while (true)
              {
                while (true)
                {
                  while (true)
                  {
                    while (true)
                    {
                      while (true)
                      {
                        if (j != 0)
                        {
                          OfferRequestPacket localOfferRequestPacket = new OfferRequestPacket(str2, str3, i, (Map)localObject2, str1, (OfferContent)localObject1);
                          localOfferRequestPacket.setType(IQ.Type.SET);
                          return localOfferRequestPacket;
                        }
                        k = paramXmlPullParser.next();
                        if (k != 2)
                          break label336;
                        str4 = paramXmlPullParser.getName();
                        if (!("timeout".equals(str4)))
                          break;
                        i = Integer.parseInt(paramXmlPullParser.nextText());
                      }
                      if (!("metadata".equals(str4)))
                        break;
                      localObject2 = MetaDataUtils.parseMetaData(paramXmlPullParser);
                    }
                    if (!("session".equals(str4)))
                      break;
                    str1 = paramXmlPullParser.getAttributeValue("", "id");
                  }
                  if (!("user".equals(str4)))
                    break;
                  str3 = paramXmlPullParser.getAttributeValue("", "id");
                }
                if (!("user-request".equals(str4)))
                  break;
                localObject1 = UserRequest.getInstance();
              }
              if (!("invite".equals(str4)))
                break;
              RoomInvitation localRoomInvitation = (RoomInvitation)PacketParserUtils.parsePacketExtension("invite", "http://jabber.org/protocol/workgroup", paramXmlPullParser);
              String str8 = localRoomInvitation.getInviter();
              String str9 = localRoomInvitation.getRoom();
              String str10 = localRoomInvitation.getReason();
              localObject1 = new InvitationRequest(str8, str9, str10);
            }
          while (!("transfer".equals(str4)));
          RoomTransfer localRoomTransfer = (RoomTransfer)PacketParserUtils.parsePacketExtension("transfer", "http://jabber.org/protocol/workgroup", paramXmlPullParser);
          String str5 = localRoomTransfer.getInviter();
          String str6 = localRoomTransfer.getRoom();
          String str7 = localRoomTransfer.getReason();
          localObject1 = new TransferRequest(str5, str6, str7);
        }
      while ((k != 3) || (!("offer".equals(paramXmlPullParser.getName()))));
      j = 1;
    }
  }

  public static class OfferRequestPacket extends IQ
  {
    private OfferContent content;
    private Map metaData;
    private String sessionID;
    private int timeout;
    private String userID;
    private String userJID;

    public OfferRequestPacket(String paramString1, String paramString2, int paramInt, Map paramMap, String paramString3, OfferContent paramOfferContent)
    {
      this.userJID = paramString1;
      this.userID = paramString2;
      this.timeout = paramInt;
      this.metaData = paramMap;
      this.sessionID = paramString3;
      this.content = paramOfferContent;
    }

    public String getChildElementXML()
    {
      StringBuilder localStringBuilder = new StringBuilder();
      localStringBuilder.append("<offer xmlns=\"http://jabber.org/protocol/workgroup\" jid=\"").append(this.userJID).append("\">");
      localStringBuilder.append("<timeout>").append(this.timeout).append("</timeout>");
      if (this.sessionID != null)
      {
        localStringBuilder.append('<').append("session");
        localStringBuilder.append(" session=\"");
        localStringBuilder.append(getSessionID()).append("\" xmlns=\"");
        localStringBuilder.append("http://jivesoftware.com/protocol/workgroup").append("\"/>");
      }
      if (this.metaData != null)
        localStringBuilder.append(MetaDataUtils.serializeMetaData(this.metaData));
      if (this.userID != null)
      {
        localStringBuilder.append('<').append("user");
        localStringBuilder.append(" id=\"");
        localStringBuilder.append(this.userID).append("\" xmlns=\"");
        localStringBuilder.append("http://jivesoftware.com/protocol/workgroup").append("\"/>");
      }
      localStringBuilder.append("</offer>");
      return localStringBuilder.toString();
    }

    public OfferContent getContent()
    {
      return this.content;
    }

    public Map getMetaData()
    {
      return this.metaData;
    }

    public String getSessionID()
    {
      return this.sessionID;
    }

    public int getTimeout()
    {
      return this.timeout;
    }

    public String getUserID()
    {
      return this.userID;
    }

    public String getUserJID()
    {
      return this.userJID;
    }
  }
}